import logger from '../core/logger';
import apiInfo from './core/apiInfo';
import current from '../core/current';
import dataInfo from './core/dataInfo';
import apiProvider from './core/apiProvider';
import { clientBase } from './core/clientBase';
import sendImages from '../model/dynamic/sendImages';
import returnModel from '../model/common/returnModel';
import dynamicModel from '../model/dynamic/dynamicModel';
import { userDynamicType } from '../enums/userDynamicType';

class dynamicClient extends clientBase {
  constructor() {
    super();
  }

  like(dynamicId: number, rid: number): Promise<returnModel<any>> {
    var self = this;
    return new Promise((resolve) => {
      try {
        let info = new dataInfo(`&dynamic_id=${dynamicId}&rid=${rid}&spec_type=0&src=bilih5&type=8&uid=${current.instance.mid}`, apiProvider.Build5);
        this.client.postResult('https://api.vc.bilibili.com/dynamic_like/v1/dynamic_like/like', info).then((res) => {
          resolve(self.buildSuccessResult<any>(res));
        });
      } catch (e) {
        logger.error(e);
        resolve(self.buildExceptionResult<any>(e));
      }
    });
  }

  getDynamicId(doc_id: string): Promise<returnModel<string>> {
    var self = this;
    return new Promise((resolve) => {
      try {
        let info = new apiInfo("http://api.vc.bilibili.com/link_draw/v2/doc/dynamic_id", `&doc_id=${doc_id}&src=bilih5`);
        this.client.getResult(info).then((res) => {
          if (res.code == '0') {
            var id = res.data.dynamic_id;
            resolve(self.buildSuccessResult<string>(id));
          } else {
            resolve(self.buildFailedResult<string>(res.message));
          }
        });
      } catch (e) {
        logger.error(e);
        resolve(self.buildExceptionResult<string>(e));
      }
    });
  }

  loadDynamicData(albumId: string): Promise<returnModel<any>> {
    var self = this;
    return new Promise((resolve) => {
      try {
        let info = new apiInfo("https://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/get_dynamic_detail", `&dynamic_id=${albumId}&src=bilih5`, true);
        this.client.getResult(info).then((res) => {
          resolve(self.buildSuccessResult<any>(res));
        });
      } catch (e) {
        logger.error(e);
        resolve(self.buildExceptionResult<any>(e));
      }
    });
  }

  repost(dynamicId: number, offset: number): Promise<returnModel<any>> {
    var self = this;
    return new Promise((resolve) => {
      try {
        let info = new dataInfo(`dynamic_id=${dynamicId}&offset=${offset}`, apiProvider.Build5);
        this.client.postResult('https://api.live.bilibili.com/dynamic_repost/v1/dynamic_repost/view_repost', info).then((res) => {
          resolve(self.buildSuccessResult<any>(res));
        });
      } catch (e) {
        logger.error(e);
        resolve(self.buildExceptionResult<any>(e));
      }
    });
  }

  getUserDynamic(userId: number, next: string): Promise<returnModel<dynamicModel>> {
    var self = this;
    return new Promise((resolve) => {
      try {
        let info = new apiInfo(`https://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/space_history`,
          `&host_uid=${userId}&offset_dynamic_id=${next}&qn=32&src=bilih5&visitor_uid=${current.instance.mid}`, true);
        this.client.getResult(info).then((res) => {
          var data: dynamicModel = res;
          resolve(self.buildSuccessResult<dynamicModel>(data));
        });
      } catch (e) {
        logger.error(e);
        resolve(self.buildExceptionResult<dynamicModel>(e));
      }
    });
  }

  loadVideoDynamic(pageIndex: number, lastDynamicId: number): Promise<returnModel<dynamicModel>> {
    var self = this;
    return new Promise((resolve) => {
      try {
        let info;
        if (lastDynamicId != -1) {
          info = new apiInfo("https://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/dynamic_history",
            `&offset_dynamic_id=${lastDynamicId}&page=${pageIndex}&qn=32&rsp_type=2&src=bili&type_list=8%2C512%2C4099&uid=${current.instance.mid}`, true);
        } else {
          info = new apiInfo("https://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/dynamic_new",
            `&qn=32&rsp_type=2&src=bili&type_list=8%2C512%2C4099&uid=${current.instance.mid}&update_num_dy_id=0`, true);
        }
        this.client.getResult(info).then((res) => {
          var data: dynamicModel = res;
          resolve(self.buildSuccessResult<dynamicModel>(data));
        });
      } catch (e) {
        logger.error(e);
        resolve(self.buildExceptionResult<dynamicModel>(e));
      }
    });
  }

  repostDynamic(dynamic_id: number, txt: string, at_uids: string, ctrl: string): Promise<returnModel<any>> {
    var self = this;
    return new Promise(async (resolve) => {
      try {
        let info = new apiInfo("https://api.live.bilibili.com/dynamic_repost/v1/dynamic_repost/repost", '', true);
        let url = await info.toString();
        let content = `uid=${current.instance.mid}&dynamic_id=${dynamic_id}&content=${encodeURIComponent(txt)}&at_uids=${at_uids}&ctrl=${encodeURIComponent(ctrl)}`;
        this.client.postResult(url, content).then((res) => {
          resolve(self.buildSuccessResult<any>(res));
        });
      } catch (e) {
        logger.error(e);
        resolve(self.buildExceptionResult<any>(e));
      }
    });
  }

  publishDynamic(send_pics: Array<sendImages>, txt: string, at_uids: string, ctrl: string): Promise<returnModel<any>> {
    var self = this;
    return new Promise((resolve) => {
      try {
        let info, content;
        if (send_pics.length == 0) {
          info = new apiInfo("https://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/create", '', true);
          content = `uid=${current.instance.mid}&dynamic_id=0&type=4&content=${encodeURIComponent(txt)}&setting=%7B%22copy_forbidden%22%3A0%7D&at_uids=${encodeURIComponent(at_uids)}&at_control=${encodeURIComponent(ctrl)}&jumpfrom=110&extension=%7B%22emoji_type%22%3A1%7D`;
        } else {
          info = new apiInfo("https://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/create_draw", '', true);
          content = `uid=${current.instance.mid}&category=3&pictures=${encodeURIComponent(JSON.stringify(send_pics))}&description=${encodeURIComponent(txt)}&content=${encodeURIComponent(txt)}&setting=%7B%22copy_forbidden%22%3A0%7D&at_uids=${encodeURIComponent(at_uids)}&at_control=${encodeURIComponent(ctrl)}&jumpfrom=110&extension=%7B%22emoji_type%22%3A1%7D`;
        }
        this.client.postResult(info, content).then((res) => {
          resolve(self.buildSuccessResult<any>(res));
        });
      } catch (e) {
        logger.error(e);
        resolve(self.buildExceptionResult<any>(e));
      }
    });
  }

  loadDynamicNew(type: userDynamicType): Promise<returnModel<dynamicModel>> {
    var self = this;
    return new Promise((resolve) => {
      try {
        var typeList = '';
        switch (type) {
          case userDynamicType.season:
            typeList = "512,4097,4098,4099,4100,4101";
            break;
          case userDynamicType.all:
            typeList = "268435455";
            break;
          case userDynamicType.video:
            typeList = "8";
            break;
          case userDynamicType.article:
            typeList = "64";
            break;
        }
        let info = new apiInfo("https://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/dynamic_new",
          `type_list=${encodeURIComponent(typeList)}&uid=${current.instance.mid}`, true);
        this.client.getResult(info).then((res) => {
          var data: dynamicModel = res;
          resolve(self.buildSuccessResult<dynamicModel>(data));
        });
      } catch (e) {
        logger.error(e);
        resolve(self.buildExceptionResult<dynamicModel>(e));
      }
    });
  }

  loadDynamicHistory(dynamic_id: string, type: userDynamicType): Promise<returnModel<dynamicModel>> {
    var self = this;
    return new Promise((resolve) => {
      try {
        let typeList = '';
        switch (type) {
          case userDynamicType.season:
            typeList = "512,4097,4098,4099,4100,4101";
            break;
          case userDynamicType.all:
            typeList = "268435455";
            break;
          case userDynamicType.video:
            typeList = "8";
            break;
          case userDynamicType.article:
            typeList = "64";
            break;
        }
        let info = new apiInfo("https://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/dynamic_history",
          `offset_dynamic_id=${dynamic_id}&type_list=${encodeURIComponent(typeList)}&uid=${current.instance.mid}`, true);
        this.client.getResult(info).then((res) => {
          var data: dynamicModel = res;
          resolve(self.buildSuccessResult<dynamicModel>(data));
        });
      } catch (e) {
        logger.error(e);
        resolve(self.buildExceptionResult<dynamicModel>(e));
      }
    });
  }
}
export default new dynamicClient();